var cc__awaiter = __awaiter;
var cc__generator = __generator;
Object.defineProperty(exports, "__esModule", {
  value: true
});
var def_ResourceLoader = function () {
  function _ctor() { }
  _ctor.getInstance = function () {
    if (!this.instance) {
      this.instance = new _ctor()
    }
    return this.instance;
  };
  _ctor.prototype.loadBundle = function (t) {
    return cc__awaiter(this, undefined, Promise, function () {
      return cc__generator(this, function () {
        return [2, new Promise(function (resolve, reject) {
          // 获取指定名称的资源包
          var bundle = cc.assetManager.getBundle(t);

          if (bundle) {
            // 如果资源包已经存在，直接解决 Promise
            resolve(bundle);
          } else {
            // 如果资源包不存在，加载它
            cc.assetManager.loadBundle(t, function (error, loadedBundle) {
              if (error) {
                // 加载失败，记录错误并拒绝 Promise
                cc.error("加载 " + t + " bundle 失败:", error);
                return reject(error);
              }
              // 加载成功，解决 Promise
              resolve(loadedBundle);
            });
          }
        })];
      });
    });
  };
  _ctor.prototype.loadBundleAsset = function (t, e, o) {
    return cc__awaiter(this, undefined, Promise, function () {
      return cc__generator(this, function () {
        return [2, new Promise(function (resolve, reject) {
          // 加载资源
          t.load(e, o, function (error, result) {
            if (error) {
              // 记录错误并拒绝 Promise
              cc.error("加载资源失败: " + e, error);
              return reject(error);
            }
            // 解决 Promise
            resolve(result);
          });
        })];
      });
    });
  };
  _ctor.prototype.loadBundleAssets = function (t, e, o) {
    return cc__awaiter(this, undefined, Promise, function () {
      var promises; // 声明 promises 变量
      var context = this; // 保存当前上下文

      return cc__generator(this, function () {
        // 映射资源列表并加载每个资源
        promises = e.map(function (asset) {
          return context.loadBundleAsset(t, asset, o); // 调用 loadBundleAsset 方法
        });

        // 返回所有 Promises 的结果
        return [2, Promise.all(promises)];
      });
    });
  };
  _ctor.prototype.load = function (t, e, o) {
    return cc__awaiter(this, undefined, Promise, function () {
      var loadedBundle; // 声明变量用于存储加载的资源包

      return cc__generator(this, function (step) {
        switch (step.label) {
          case 0:
            // 加载指定的资源包
            return [4, this.loadBundle(t)];
          case 1:
            loadedBundle = step.sent(); // 获取加载的资源包
            // 加载资源包中的指定资产
            return [2, this.loadBundleAsset(loadedBundle, e, o)];
        }
      });
    });
  };
  _ctor.prototype.releaseAsset = function (t) {
    if (t) {
      t.decRef();
      cc.assetManager.releaseAsset(t);
    }
  };
  _ctor.prototype.releaseBundle = function (t) {
    // 检查 t 是否为字符串类型
    if (typeof t === "string") {
      // 获取指定名称的资源包
      var bundle = cc.assetManager.getBundle(t);
      if (bundle) {
        // 释放所有资源并移除资源包
        bundle.releaseAll();
        cc.assetManager.removeBundle(bundle);
      }
    } else {
      // 如果 t 不是字符串，直接释放资源并移除资源包
      t.releaseAll();
      cc.assetManager.removeBundle(t);
    }
  };
  _ctor.prototype.loadBundleDir = function (t, e, o) {
    return cc__awaiter(this, undefined, Promise, function () {
      return cc__generator(this, function () {
        return [2, new Promise(function (resolve, reject) {
          // 加载指定目录
          t.loadDir(e, o, function (error, assets) {
            if (error) {
              // 记录错误并拒绝 Promise
              cc.error("加载目录失败: " + e, error);
              return void reject(error);
            }
            // 解决 Promise
            resolve(assets);
          });
        })];
      });
    });
  };
  _ctor.prototype.loadDir = function (t, e, o) {
    return cc__awaiter(this, undefined, Promise, function () {
      var loadedBundle; // 声明变量用于存储加载的资源包

      return cc__generator(this, function (step) {
        switch (step.label) {
          case 0:
            // 加载指定的资源包
            return [4, this.loadBundle(t)];
          case 1:
            loadedBundle = step.sent(); // 获取已加载的资源包
            // 加载资源包中的指定目录
            return [2, this.loadBundleDir(loadedBundle, e, o)];
        }
      });
    });
  };
  _ctor.prototype.preloadBundleAsset = function (t, e, o) {
    return cc__awaiter(this, undefined, Promise, function () {
      return cc__generator(this, function () {
        return [2, new Promise(function (resolve, reject) {
          t.preload(e, o, function (error) {
            if (error) {
              cc.error(`预加载资源失败: ${e}`, error);
              return void reject(error);
            }
            resolve();
          });
        })];
      });
    });
  };
  _ctor.prototype.preloadBundleAssets = function (t, e, o) {
    return cc__awaiter(this, undefined, Promise, function () {
      var preloadPromises;
      var context = this;

      return cc__generator(this, function () {
        preloadPromises = e.map(function (asset) {
          return context.preloadBundleAsset(t, asset, o);
        });

        return [2, Promise.all(preloadPromises).then(function () { })];
      });
    });
  };
  _ctor.prototype.preloadBundleDir = function (t, e, o) {
    return cc__awaiter(this, undefined, Promise, function () {
      return cc__generator(this, function () {
        return [2, new Promise(function (resolve, reject) {
          t.preloadDir(e, o, function (error) {
            if (error) {
              cc.error(`预加载目录失败: ${e}`, error);
              return void reject(error);
            }
            resolve();
          });
        })];
      });
    });
  };
  _ctor.prototype.preload = function (t, e, o) {
    return cc__awaiter(this, undefined, Promise, function () {
      var bundle;

      return cc__generator(this, function (context) {
        switch (context.label) {
          case 0:
            return [4, this.loadBundle(t)];
          case 1:
            bundle = context.sent();
            return [2, this.preloadBundleAsset(bundle, e, o)];
        }
      });
    });
  };
  _ctor.prototype.preloadDir = function (t, e, o) {

    return cc__awaiter(this, undefined, Promise, function () {
      var bundle;

      return cc__generator(this, function (context) {
        switch (context.label) {
          case 0:
            return [4, this.loadBundle(t)];
          case 1:
            bundle = context.sent();
            return [2, this.preloadBundleDir(bundle, e, o)];
        }
      });
    });
  };
  _ctor.prototype.preloadAssets = function (t, e, o) {
    return cc__awaiter(this, undefined, Promise, function () {
      var bundle;

      return cc__generator(this, function (context) {
        switch (context.label) {
          case 0:
            return [4, this.loadBundle(t)];
          case 1:
            bundle = context.sent();
            return [2, this.preloadBundleAssets(bundle, e, o)];
        }
      });
    });
  };
  _ctor.instance = null;
  return _ctor;
}();
exports.default = def_ResourceLoader;